﻿using Dao;
using Infrastructure.Model;
using Infrastructure.MyException;
using ShareCode.Dto;
using ShareCode.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bll
{
    public class BaseBaseBll<T> : IBaseBll<T> where T : BaseBaseEntity, new()
    {
        protected BaseBaseDao<T>? _Dao;

        protected virtual BaseBaseDao<T>? Dao { get { return _Dao; } }

        #region 实现接口

        public BaseBaseDao<T>? GetDao()
        {
            return _Dao;
        }

        public virtual bool Verify(T param, bool isUpdate)
        {
            if (isUpdate && param.Id < 1)
            {
                throw new ShowUserLogicException("没有指定要更新的目标").AddSonMessage(nameof(param.Id), "没有指定要更新的目标");
            }
            return true;
        }

        #endregion



        public async virtual Task<long> InsertAsync(T model, bool verify = true)
        {

            Verify(model, false);


            _ = await Dao!.InsertAsync(model);

            return model.Id;
        }

        public async virtual Task<bool> UpdateAsync(T model, bool verify = true)
        {
            Verify(model, true);

            var a = await Dao!.UpdateAsync(model);

            return a;
        }

        public async virtual Task<bool> DeleteByIdAsync(long Id)
        {
            return await DeleteByIdAsync([Id]);
        }

        public async virtual Task<bool> DeleteByIdAsync(List<long> Ids)
        {
            return await Dao!.AsDeleteable().Where(t => Ids.Contains(t.Id)).ExecuteCommandAsync() > 0;
        }

        public async virtual Task<bool> UpdateStateAsync(long Id, int state)
        {
            return await Dao!.UpdateStateAsync(Id, state);
        }
        public async virtual Task<bool> UpdateStateAsync(List<long> Ids, int state)
        {
            return await Dao!.UpdateStateAsync(Ids, state);
        }

        public async virtual Task<bool> UnLogicDeleteByIdAsync(long Id)
        {
            return await Dao!.UnLogicDeleteByIdAsync(Id);
        }

        public async virtual Task<bool> UnLogicDeleteByIdAsync(List<long> Ids)
        {
            return await Dao!.UnLogicDeleteByIdAsync(Ids);
        }

        public async virtual Task<bool> LogicDeleteByIdAsync(long Id)
        {
            return await Dao!.LogicDeleteByIdAsync(Id);

        }
        public async virtual Task<bool> LogicDeleteByIdAsync(List<long> Ids)
        {
            return await Dao!.LogicDeleteByIdAsync(Ids);
        }
        public async virtual Task<T> GetEntityAsync(long id)
        {
            return await Dao!.GetFirstAsync(t => t.Id == id);
        }

        public async virtual Task<PagingWithDataModel<T>> ListPageAsync(PagingDto? dto)
        {
            return await Dao!.ListPageAsync(dto);
        }

        public async virtual Task<List<T>> ListAsync(PagingDto? dto)
        {
            return await Dao!.ListAsync(dto);
        }

        public async virtual Task<PagingWithDataModel<T>> ListPageNoDeleteAsync(PagingDto? dto)
        {
            dto ??= new PagingDto();
            dto.SearchField.TryAdd(nameof(BaseBaseEntity.IsDelete), "0");

            return await Dao!.ListPageAsync(dto);
        }

        public async virtual Task<List<T>> ListNoDeleteAsync(PagingDto? dto)
        {
            dto ??= new PagingDto();
            dto.SearchField.TryAdd(nameof(BaseBaseEntity.IsDelete), "0");

            return await Dao!.ListAsync(dto);
        }


    }
}
